﻿using System;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class BoardingPass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("DA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["bk_id"] == null || Request.QueryString["pax_list"] == null || Request.QueryString["flt_date"] == null)
                {
                    lblError.Text = "Illegal page call!";
                    btnPrint.Visible = false;
                }
                else
                {
                    Retreive();
                    btnPrint.Attributes.Add("onclick", "javascript:window.print();");
                }
            }
        }

        protected void Retreive()
        {
            int ret_code;
            string pax_list;
            string[] pax;
            ret_code = num_list.parse(Request.QueryString["pax_list"], out pax_list);
            if (ret_code < 0)
            {
                lblError.Text = "Bad format!";
                return;
            }
            pax = pax_list.Split(',');

            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            DataTable dtTicket = new DataTable();
            DataRow row;
            string buf, pass;
            StringBuilder sOutput = new StringBuilder();

            cmd.CommandText = "bll_tkt_get_boarding_pass_info";

            for (int i = 0; i < pax.GetLength(0); i++)
            {
                cmd.Parameters.Clear();
                para = cmd.Parameters.AddWithValue("@bk_id", Request.QueryString["bk_id"]);
                para = cmd.Parameters.AddWithValue("@pax_sn", pax[i]);
                //para = cmd.Parameters.AddWithValue("@trip_sn", Request.QueryString["trip_sn"]);
                para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_date", Request.QueryString["flt_date"]);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                TinyDAL.Exec(ref cmd, ref dtTicket);
                if (dtTicket.Rows.Count > 0)
                {
                    row = dtTicket.Rows[0];
                    buf = "000" + row["pax_sn"].ToString();
                    buf = buf.Substring(buf.Length - 3);
                    pass = row["bk_id"].ToString() + buf + "/" + row["trip_sn"].ToString();
                    sOutput.Append("<table border='0'><tr><th colspan='2'>登机牌<br/>Boradring Pass</th></tr><tr><td>&nbsp</td></tr>");
                    sOutput.Append("<tr><td>姓名<br/>Name</td><td><b>" + row["family_name"].ToString().Trim() + "/" + row["name"].ToString().Trim());
                    sOutput.Append("<br/>" + row["family_name_en"].ToString().Trim() + "/" + row["name_en"].ToString().Trim() + "</b></td></tr>");
                    sOutput.Append("<tr><td>航班<br/>Flight</td><td><b>" + row["airline"].ToString() + row["flt_no"].ToString());
                    if (Convert.ToBoolean(row["virtual"]))
                    {
                        sOutput.Append("(" + row["op_airline"].ToString() + row["op_flt_no"].ToString() + ")");
                    }
                    sOutput.Append("</b></td></tr>");
                    sOutput.Append("<tr><td>日期<br/>Date</td><td><b>" + Convert.ToDateTime(row["flt_date"]).ToShortDateString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>航程<br/>Trip</td><td><b>" + row["ori_airport"].ToString().Trim() + " - " + row["des_airport"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>舱号<br/>Cabin</td><td><b>" + row["cabin"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>年龄<br/>Age</td><td><b>" + row["age_type"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>登机口<br/>Gate</td><td><b>" + row["boarding_gate"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>登机时间<br/>Boarding Time</td><td><b>" + Convert.ToDateTime(row["boarding_time"]).ToShortTimeString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>座位<br/>Seat</td><td><b>" + row["seat_row"].ToString() + row["seat_col"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>证件号<br/>ID</td><td><b>" + row["id_no"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>登机牌号<br/>Pass No.</td><td><b>" + pass + "</b></td></tr>");
                    sOutput.Append("<tr><td colspan='2'><img src='barcode.ashx?source=" + pass + "&height=30&unit_width=1&vertical=F'></td></tr>");

                    sOutput.Append("<tr><td>&nbsp;</td></tr><tr><td colspan='2'><hr/></td></tr><tr><td>&nbsp;</td></tr>");

                    sOutput.Append("<tr><td>Name</td><td><b>" + row["family_name"].ToString().Trim() + "/" + row["name"].ToString().Trim());
                    sOutput.Append("<br/>" + row["family_name_en"].ToString().Trim() + "/" + row["name_en"].ToString().Trim() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Flight</td><td><b>" + row["airline"].ToString() + row["flt_no"].ToString());
                    if (Convert.ToBoolean(row["virtual"]))
                    {
                        sOutput.Append("(" + row["op_airline"].ToString() + row["op_flt_no"].ToString() + ")");
                    }
                    sOutput.Append("</b></td></tr>");
                    sOutput.Append("<tr><td>Date</td><td><b>" + Convert.ToDateTime(row["flt_date"]).ToShortDateString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Trip</td><td><b>" + row["ori_airport"].ToString().Trim() + " - " + row["des_airport"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Cabin</td><td><b>" + row["cabin"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Age</td><td><b>" + row["age_type"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Gate</td><td><b>" + row["boarding_gate"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Boarding Time</td><td><b>" + Convert.ToDateTime(row["boarding_time"]).ToShortTimeString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Seat</td><td><b>" + row["seat_row"].ToString() + row["seat_col"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>ID</td><td><b>" + row["id_no"].ToString() + "</b></td></tr>");
                    sOutput.Append("<tr><td>Pass No.</td><td><b>" + pass + "</b></td></tr>");
                    sOutput.Append("<tr><td colspan='2'><img src='barcode.ashx?source=" + pass + "&height=30&unit_width=1&vertical=F'></td></tr></table>");

                    if (i < pax.GetLength(0) - 1)
                    {
                        sOutput.Append("<div style='PAGE-BREAK-AFTER: always'></div>");
                    }
                }
                else
                {
                    lblError.Text = "Not found!";
                    return;
                }
                //btnPrint.Visible = true;
                //imgBar.Visible = true;
                //imgBar2.Visible = true;
                //DataRow row = dtTicket.Rows[0];
                //lblAirline.Text = row["airline"].ToString();
                //lblFltNo.Text = row["flt_no"].ToString();
                //lblDate.Text = Convert.ToDateTime(row["flt_date"]).ToShortDateString();
                //lblCabin.Text = row["cabin"].ToString();
                //lblClass.Text = "";// row["class"].ToString();
                //lblName.Text = row["family_name"].ToString().Trim() + "/" + row["name"].ToString().Trim();
                //lblNameEn.Text = row["family_name_en"].ToString().Trim() + "/" + row["name_en"].ToString().Trim();
                //lblAge.Text = row["age_type"].ToString();
                //lblTrip.Text = row["ori_airport"].ToString().Trim() + " - " + row["des_airport"].ToString();
                //lblGate.Text = row["boarding_gate"].ToString();
                //lblSeat.Text = row["seat_row"].ToString() + row["seat_col"].ToString();
                //if (Convert.IsDBNull(row["boarding_time"]))
                //    lblBoardingTime.Text = "";
                //else
                //    lblBoardingTime.Text = Convert.ToDateTime(row["boarding_time"]).ToShortTimeString();
                //buf = "000" + row["pax_sn"].ToString();
                //buf = buf.Substring(buf.Length - 3);
                //lblTktNo.Text = row["bk_id"].ToString() + buf;
                //lblIdNo.Text = row["id_no"].ToString();
                //imgBar.ImageUrl = "barcode.ashx?source=" + lblIdNo.Text + "&height=30&unit_width=1&vertical=F";


                //lblAirline2.Text = lblAirline.Text;
                //lblFltNo2.Text = lblFltNo.Text;
                //lblDate2.Text = lblDate.Text;
                //lblCabin2.Text = lblCabin.Text;
                //lblClass2.Text = "";//lblClass.Text;
                //lblName2.Text = lblName.Text;
                //lblNameEn2.Text = lblNameEn.Text;
                //lblAge2.Text = lblAge.Text;
                //lblTrip2.Text = lblTrip.Text;
                //lblGate2.Text = lblGate.Text;
                //lblSeat2.Text = lblSeat.Text;
                //lblBoardingTime2.Text = lblBoardingTime.Text;
                //lblTktNo2.Text = lblTktNo.Text;
                //lblIdNo2.Text = lblIdNo.Text;
                //imgBar2.ImageUrl = "barcode.ashx?source=" + lblIdNo.Text + "&height=30&unit_width=1";
            }
            litOutput.Text = sOutput.ToString();
        }
    }
}